<?php
/**
 * author:歪妻
 * Date: 2020/11/2 0002
 */

require_once 'lib/common.php';
require_once 'lib/view-post.php';
$postId = postId();
$pdo = getPDO();
$row = getView($pdo,$postId);

//如果帖子不存在，我们在这里处理
if (!$row)
{
    redirectAndExit('index.php?not-found=1');
}
// 交换回车符
$bodyText = htmlEscape($row['body']);
$paraText = str_replace("\n", "</p><p>", $bodyText);
?>

<!DOCTYPE html>
<html>
<head>
    <title>
        A blog application |
        <?php echo htmlEscape($row['title']) ?>
    </title>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>
<body>
<?php require 'templates/title.php' ?>

<h2>
    <?php echo htmlEscape($row['title']) ?>
</h2>

<div>
    <?php echo convertSqlDate($row['created_at']) ?>
</div>
<p>
    <?php echo $paraText ?>
</p>
<h3><?php echo countCommentsForPost($postId) ?> 评论</h3>

<?php foreach (getCommentsForPost($postId) as $comment): ?>
    <hr />
    <div class="comment">
        <div class="comment-meta">
            评论 来自
            <?php echo htmlEscape($comment['name']) ?>
            at
            <?php echo convertSqlDate($comment['created_at']) ?></a>
        </div>
        <div class="comment-body">
            <a href="<?php echo htmlEscape($comment['website'])?>">个人中心</a>
        </div>
        <div class="comment-body">
            <?php echo htmlEscape($comment['text']) ?>
        </div>
    </div>
<?php endforeach ?>
</body>
</html>